package com.example.tourstor;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;


public class TravelTimeActivity extends AppCompatActivity {
    private static final String TAG = "TravelTimeActivity";
    private TextView title,showt1,showt2,showt3;
    private Button title_btn_left,title_btn_right;
    private CalendarView mCalendarView;
    private Calendar calender;
    private Connection con ;
    private PreparedStatement ps=null,ps1;//操作整合sql语句的对象
    private ResultSet rs=null;
    private Thread t;
    private Date date;
    DBOpenHelper dbOpenHelper;
    private String name;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_travel_time);
        setTitle();

        Intent intent=getIntent();
        name=intent.getStringExtra("Cname");

        title_btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t=new Thread(new Runnable() {
                    @Override
                    public void run() {

                        Handler handler = new Handler(Looper.getMainLooper());
                        con = dbOpenHelper.getConn();

                        String sql2="select * from makeplan where Uid= '"+name+"' and  datediff ( Tstart , '"+date+"' ) = 0 ";
                        try {
                            ps1= (PreparedStatement) con.prepareStatement(sql2);
                            rs= ps1.executeQuery();
                            Log.i(TAG, "run: "+ps1+rs);
                            rs.last();
                            int count=rs.getRow();
                            Log.i(TAG, "run: 查询到的记录数是："+count);

                            if(count==1){
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(TravelTimeActivity.this,"该计划已经存在",Toast.LENGTH_LONG).show();
                                    }
                                });

                            }else {

                                SharedPreferences sharedPreferences=getSharedPreferences("Tstart", Activity.MODE_PRIVATE);
                                SharedPreferences.Editor editor=sharedPreferences.edit();
                                editor.putString("Tstart", String.valueOf(date));
                                editor.commit();
                                Log.i(TAG, "run: 选择的日期已经存入sharedPreferences中");

                                Intent intent = new Intent(TravelTimeActivity.this,MakePlanActivity.class);
                                intent.putExtra("Tstart",String.valueOf(date));
                                setResult(11, intent);
                                finish();

                            }

                            con.close();//一定要关闭
                            ps1.close();
                            rs.close();

                        } catch (SQLException e) {
                            e.printStackTrace();
                        }

                    }
                });
                t.start();

            }
        });

        showt1=findViewById(R.id.showtime1);
        showt2=findViewById(R.id.showtime2);
        showt3=findViewById(R.id.showtime3);
        mCalendarView=findViewById(R.id.calendar);
        //初始化日期
        calender = Calendar.getInstance();
        /*String datat = calender.get(Calendar.YEAR) + "-" + (calender.get(Calendar.MONTH)+1)
                + "-" + calender.get(Calendar.DAY_OF_MONTH) ;*/
        java.util.Date date2=calender.getTime();
        date=new java.sql.Date(date2.getTime());
        Log.i(TAG, "onCreate: 当前的时间是："+date);

        String mWay = String.valueOf(calender.get(Calendar.DAY_OF_WEEK));
        if("1".equals(mWay)){
            mWay ="天";
        }else if("2".equals(mWay)){
            mWay ="一";
        }else if("3".equals(mWay)){
            mWay ="二";
        }else if("4".equals(mWay)){
            mWay ="三";
        }else if("5".equals(mWay)){
            mWay ="四";
        }else if("6".equals(mWay)){
            mWay ="五";
        }else if("7".equals(mWay)){
            mWay ="六";
        }
        showt1.setText(String.valueOf(calender.get(Calendar.DAY_OF_MONTH) < 10 ? "0" + calender.get(Calendar.DAY_OF_MONTH) : calender.get(Calendar.DAY_OF_MONTH)));
        showt2.setText(String.valueOf((calender.get(Calendar.MONTH) + 1) < 10 ? "0" + (calender.get(Calendar.MONTH) + 1) : (calender.get(Calendar.MONTH) + 1))+" 月");
        showt3.setText("星期"+mWay);
        chooseCalender();
    }
    private void chooseCalender() {
        mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                String dates = year + "-" + ((month + 1) < 10 ? "0" + (month + 1) : (month + 1)) + "-" + (dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth);
                date= Date.valueOf(dates);
                Log.i(TAG, "onSelectedDayChange: 选择的时间是："+date);

                calender.set(year, month, dayOfMonth);
                int cweek=calender.get(Calendar.DAY_OF_WEEK);
                String mWay = String.valueOf(cweek);
                if("1".equals(mWay)){
                    mWay ="天";
                }else if("2".equals(mWay)){
                    mWay ="一";
                }else if("3".equals(mWay)){
                    mWay ="二";
                }else if("4".equals(mWay)){
                    mWay ="三";
                }else if("5".equals(mWay)){
                    mWay ="四";
                }else if("6".equals(mWay)){
                    mWay ="五";
                }else if("7".equals(mWay)){
                    mWay ="六";
                }
                showt1.setText(String.valueOf(dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth));
                showt2.setText(String.valueOf((month + 1) < 10 ? "0" + (month + 1) : (month + 1))+" 月");
                showt3.setText("星期"+mWay);
            }
        });

    }

    public void setTitle(){
        title = findViewById(R.id.title_text);
        title.setText("选择时间");
        title_btn_left = findViewById(R.id.title_btn_left);
        title_btn_right=findViewById(R.id.title_btn_right);
        title_btn_left.setText("");
        /*Drawable drawable = getResources().getDrawable(R.drawable.left_arrow);
        drawable.setBounds(0, 0, 80, 80);//第一0是距左边距离，第二0是距上边距离，40分别是长宽
        //editText1.setCompoundDrawables(drawable, null, null, null);//只放左边
        title_btn_left.setBackgroundDrawable(drawable);*/
        title_btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
